home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10272 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: David Brownell <brownell@ix.netcom.com>
  3. Newsgroups: comp.programming.threads,comp.lang.c++,comp.unix.osf.osf1,comp.unix.programmer,comp.object
  4. Subject: Re: Looking for best design for using pthreads in C++ objects
  5. Date: Wed, 06 Mar 1996 10:01:29 -1000
  6. Organization: Dave's VAX
  7. Message-ID: <313DEF19.5B69@ix.netcom.com>
  8. References: <3128ff8b.666031216@news.clark.net> <312A0E5F.7B2C@ix.netcom.com> <31320705.41C6@zko.dec.com>
  9. NNTP-Posting-Host: haw-ak1-06.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-NETCOM-Date: Wed Mar 06 12:05:18 PM PST 1996
  14. X-Mailer: Mozilla 2.0 (Win95; I)
  15.  
  16. Webb Scales wrote:
  17. > David Brownell wrote:
  18. > > One of the really nice techniques is to have a "Locker"
  19. > > class to grab mutexes
  20. > > as needed, and then release it automatically on all exits.  [...]
  21. > > That kind of class really helps get rid of the bugs you have due
  22. > > to locks not getting released uniformly on all codepaths.
  23. > Hmmm...this approach makes me nervous.  I assert that it's better
  24. > to neglect to unlock a mutex than to unlock it when you shouldn't have.
  25.  
  26. Depends on how you're using the mutex; both bugs are deadly.  Did the
  27. victim die because of the loss of blood first, or the kidney failure?
  28. (Don't you just hate it when police beat up suspects?)
  29.  
  30.  
  31. > For instance, if a thread holding a mutex via the "Locker" class terminates
  32. > prematurely (due to an exception or cancellation), then the mutex will be
  33. > unlocked automagically, despite the fact that whatever it was supposed to
  34. > protect is now likely to be in an inconsistent state.
  35.  
  36. Huh???  What kind of buggy algorithm are you assuming?
  37.  
  38. Cleanup processing for exceptions/cancellation is required to leave
  39. the resources in a consistent state ... else the cleanup processing
  40. is buggy.  The ** whole point ** of the cleanup processing (destructor
  41. handling etc) is to keep state consistent.  Any C++ instructor who is
  42. not making this clear needs to be immediately replaced.
  43.  
  44. Now ... given that C++ stack cleanup is done on each function call
  45. exit, and hence isn't specific to exceptions/cancellation, I'd claim
  46. that bugs in cleanup with a "Locker" class are found VERY fast.  I
  47. can support that with my own experience, FWIW!
  48.  
  49.  
  50. > The missing piece is that the destructor for Locker, prior to releasing the
  51. > mutex, must restore the shared data that the mutex protects to a consistent
  52. > state.  In order to do this, the destructor requires intimate knowledge
  53. > of what the lock is protecting -- this invalidates the "generic" approach.
  54.  
  55. Well, I never said that you would _only_ have a "Locker" class!  I called
  56. it a _technique_ ... meaning, "modify as appropriate".  You're talking as
  57. if I'd said "here's a complete general purpose library", which I didn't.
  58. I'd claim there is no such critter, in fact.
  59.  
  60. Typical programming style for mutexes is that functions have an
  61. invariant:  the mutex state doesn't change as a consequence of calling
  62. the function.  The "Locker" class I posted did that just for mutexes,
  63. since that generalizes naturally and is an appropriate starting point
  64. for such discussions.
  65.  
  66. More complex invariants can be encapsulated in other classes rather
  67. easily, and should be.  As in fact Webb noted later.  I didn't have time
  68. to write out a complete C++/MT style guide; that point is a logical
  69. combination of (a) encapsulation, and (b) the "Locker" technique.
  70.  
  71. - Dave
  72.